package com.yylending.las.common.location;

import android.text.TextUtils;
import com.google.gson.Gson;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yylending.las.Global;
import com.yylending.las.YJKD;
import com.yylending.las.common.location.entity.Location;
import com.yylending.las.common.utils.preferenceutils.PreferenceConstants;

/**
 * Created by laixj on 2016/11/10.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class LocationPresenter extends Presenter<Model, LocationPresenter.View> {
	private final static String TAG = "LocationPresenter";

	public static class Generator implements PresenterFactory<LocationPresenter> {
		@Override
		public LocationPresenter create() {
			return new LocationPresenter();
		}
	}

	public interface View extends IView {
		void onLocateSuccess(Location location);

		void onLocateFail(boolean noPermission);

		void showDialog();

		void dismissDialog();
	}

	private View mView;
	private String mRequestPage;
	/**
	 * 是否已经回调过
	 */
	private boolean mReturned = false;

	private LocationService mLocationService;
	private MyLocationListener mListener;
	private boolean mShowLoading = false;
	/**
	 * 是否已重试
	 */
	private boolean mHaveRetried = false;

	@Override
	public void attach(View view) {
		CLog.i(TAG, "attach mView=" + view.getClass().getName());
		mRequestPage = view.getClass().getName();
		mView = view;
		mLocationService = YJKD.getInstance().locationService;
		mListener = new MyLocationListener(mRequestPage);
	}

	@Override
	public void detach() {
		CLog.i(TAG, "detach mView=" + mView.getClass().getName());
		if (mShowLoading) mView.dismissDialog();
		mView = null;
		//mLocationService.unregisterListener(mListener); //注销掉监听
		CLog.i(TAG, "取消定位监听-detach 停止定位服务-");
		mLocationService.stopLocation(); //停止定位服务
	}

	/**
	 * 开始获取位置信息
	 */
	public void startLocation() {
		mShowLoading = true;
		mReturned = false;
		mView.showDialog();
		CLog.i(TAG, "注册定位监听器并开始定位");
		mLocationService.registerListener(mListener);
		mLocationService.startLocation();
	}

	/**
	 * 开始获取位置信息
	 */
	public void startLocation(boolean showDialog) {
		mShowLoading = showDialog;
		mReturned = false;
		if (mShowLoading) mView.showDialog();
		CLog.i(TAG, "注册定位监听器并开始定位");
		mLocationService.registerListener(mListener);
		mLocationService.startLocation();
	}

	/**
	 * 停止获取位置信息
	 */
	private void stopLocation() {
		//CLog.i(TAG, "LocationPresenter stopLocation()");
		//mLocationService.unregisterListener(mListener); //注销掉监听
		//CLog.i(TAG, "stopLocation 停止定位服务-");
		//mLocationService.stopLocation(); //停止定位服务
	}

	/**
	 * 定位结果回调
	 */
	private class MyLocationListener implements LocationListener {
		private String mPage;

		public MyLocationListener(String mRequestPage) {
			this.mPage = mRequestPage;
		}

		@Override
		public void onLocateSuccess(Location location) {
			if (null != mView && mRequestPage.equals(mPage)) {
				mHaveRetried = false;
				if (mReturned) {
					return;
				}
				mReturned = true;
				Global.mLongitude = location.longitude;
				Global.mLatitude = location.latitude;
				saveLocation(location);
				if (mShowLoading)
					mView.dismissDialog();
				mView.onLocateSuccess(location);
			}
		}

		@Override
		public void onLocateFailure(boolean noPermission, boolean needRetry) {
			if (null != mView && mRequestPage.equals(mPage)) {
				if (mReturned) {
					return;
				}
				mLocationService = YJKD.getInstance().locationService;
				mReturned = true;
				clearSavedLocation();
				if (needRetry && !mHaveRetried) {//重试
					mHaveRetried = true;
					startLocation(mShowLoading);
				} else {
					mHaveRetried = false;
					if (mShowLoading)
						mView.dismissDialog();
					mView.onLocateFail(noPermission);
				}
			}
		}

		@Override
		public boolean equals(Object o) {
			if (this == o)
				return true;
			if (o == null || getClass() != o.getClass())
				return false;

			MyLocationListener that = (MyLocationListener) o;
			return mPage != null ? mPage.equals(that.mPage) : that.mPage == null;
		}

		@Override
		public int hashCode() {
			return mPage != null ? mPage.hashCode() : 0;
		}
	}

	public static Location getSavedLocation() {
		Location location = null;
		String object = YJKD.getInstance().getPreferenceManager().getLocationInfo();
		if (!TextUtils.isEmpty(object) && !"{}".equals(object)) {
			Gson gson = new Gson();
			location = gson.fromJson(object, Location.class);
		}
		return location;
	}

	private static void saveLocation(Location location) {
		CLog.i(TAG, "定位成功：" + location.address.province + "、" + location.address.city + "、" + location.address.district);
		Gson gson = new Gson();
		String locationInfo = gson.toJson(location, Location.class);
		if (null != locationInfo) {
			CLog.i(TAG, "locationInfo=" + locationInfo);
			YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.KEY_LOCATION_INFO, locationInfo);
		}
	}

	public static void clearSavedLocation() {
		YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.KEY_LOCATION_INFO, "");
	}
}